package com.arges.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arges.web.common.Constant;
import com.arges.web.common.GsonUtils;
import com.arges.web.common.SysEnum.Zlfs;
import com.arges.web.domain.bean.TreatBean;
import com.arges.web.domain.pojo.Treat;
import com.arges.web.page.PageInfo;
import com.arges.web.page.WebUtils;
import com.arges.web.service.PatManager;
import com.arges.web.service.TreatManager;

/**
 * doctor控制层
 * 
 * @author zxj
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/treat")
public class TreatController
{

	private Logger LOGGER = LoggerFactory.getLogger(TreatController.class);

	@Autowired
	private TreatManager treatManager;

	@Autowired
	private PatManager patManager;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getPageTreat.do")
	public String getPageTreat(HttpServletRequest request, ModelMap map)
	{

		PageInfo<TreatBean> pageInfo = null;
		String mrn = request.getParameter("search_mrn");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		if (!StringUtils.isNotBlank(mrn))
		{
			mrn = "";
		}
		if (!StringUtils.isNotBlank(startDate))
		{
			startDate = "";
		}
		if (!StringUtils.isNotBlank(endDate))
		{
			endDate = "";
		}

		String pageString = request.getParameter("PageInfo");
		if (StringUtils.isNotBlank(pageString))
		{
			pageInfo = GsonUtils.stringToObject(pageString, PageInfo.class);
		}
		else
		{
			pageInfo = new PageInfo<TreatBean>();
		}

		try
		{
			PageInfo<Treat> pojopInfo = WebUtils.beanToPojoPage(pageInfo, Treat.class);
			pageInfo = treatManager.getPageTreat(pojopInfo, mrn, startDate, endDate);
		}
		catch (Exception e)
		{
			System.out.println("getPageTreat is error. e=" + e);
		}
		map.put("chaxun", mrn);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("treatBeanList", pageInfo.getResult());
		pageInfo.setResult(null);
		map.put("treatPageInfo", GsonUtils.objectToJson(pageInfo));
		return "pat/treat";
	}

	/**
	 * 获取诊断治疗列表
	 * 
	 * @param request
	 * @return list
	 */
	@RequestMapping(value = "/getZlfsList.do")
	@ResponseBody
	public Zlfs[] getZlfsList()
	{
		return Zlfs.values();
	}

	@RequestMapping(value = "/saveOrUpdate.do")
	@ResponseBody
	public String saveOrUpdate(HttpServletRequest request, TreatBean treatBean)
	{
		String result = Constant.SUCCESS;

		Zlfs zlfs = Zlfs.valueOf(treatBean.getTherary());
		treatBean.setTherary(String.valueOf(zlfs.getId()));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try
		{
			Date date = sdf.parse(treatBean.getT_date_String());
			treatBean.setT_date(date);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		if (null != treatBean.getId())
		{
			Treat treat = new Treat();
			BeanUtils.copyProperties(treatBean, treat);
			treatManager.update(treat);
		}
		else
		{
			Treat treat = new Treat();
			BeanUtils.copyProperties(treatBean, treat);
			treatManager.save(treat);
		}

		return result;
	}

	/**
	 * 根据病历号获取患者信息
	 *
	 * @param mrn
	 * @return
	 */
	@RequestMapping(value = "/getTreatById.do")
	@ResponseBody
	public TreatBean getTreatById(HttpServletRequest request)
	{
		String id = request.getParameter("id");
		TreatBean treatBean = new TreatBean();
		// try
		// {
		Treat treat = treatManager.get(Long.valueOf(id));
		BeanUtils.copyProperties(treat, treatBean);
		Zlfs[] zlfsList = Zlfs.values();
		for (Zlfs zlfs : zlfsList)
		{
			if (treatBean.getTherary().equals(String.valueOf(zlfs.getId())))
			{
				treatBean.setTherary(zlfs.name());
			}
		}

		// // }
		// // catch (Exception e)
		// {
		// System.out.println("getTreatByMrn is error,error is" + e);
		// // treatBean = null;
		// //// }
		return treatBean;
	}

	/**
	 * 删除、批量删除用户
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteByIds.do")
	@ResponseBody
	public String deleteByIds(HttpServletRequest request)
	{
		String result = Constant.SUCCESS;
		String ids = request.getParameter("ids");
		try
		{
			treatManager.deleteByIds(ids);
		}
		catch (Exception e)
		{
			System.out.println("delete treat ByIds is error,error is" + e);
			result = Constant.ERROR;
		}
		return result;
	}
}
